//
//  WYHomeController.m
//  WYNews
//
//  Created by 曾佳豪 on 2017/3/18.
//  Copyright © 2017年 haodada.com. All rights reserved.
//

#import "WYHomeController.h"
#import "WYChannelView.h"
#import <Masonry.h>
#import "WYNewListController.h"
@interface WYHomeController ()<UIPageViewControllerDataSource,WYChannelViewDelegate,UIPageViewControllerDelegate>{

    WYChannelView *channelView;
    //全局pageView控制器
    UIPageViewController *pageViewController;
}

@end

@implementation WYHomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets =NO;
    //添加频道视图
    [self addChannelView];
    
    //添加页
    [self setPageViewController];
}

//添加频道视图
-(void)addChannelView{
    channelView =[WYChannelView new];
    //设置代理
    channelView.channelDelegate =self;
    [self.view addSubview: channelView];
    [channelView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.right.equalTo(self.view);
        make.top.mas_equalTo(60);
        make.height.mas_equalTo(40);
    }];
}

//实现代理方法(切换频道delegate)
//参数 对应的频道的index (点击label)
-(void)channelChanged:(NSInteger)index{
    
    NSLog(@"切换到频道:%zd",index);
    
        //重新设置pageViewController的子控制器
    //1. 获取到当前的子控器的index
    //1.1先取到当前控制器 (第0个元素)
    WYNewListController *newList =(WYNewListController *)pageViewController.viewControllers[0];
    //
    NSInteger currentIndex =newList.index;
    //2.判断  当前的index 和点击的index一致 不动
    if ( currentIndex ==index) return;
    
   //3.如果点击的index 比当前index大 从右往左推  如果较小 从左往右推.
    NSInteger direction =currentIndex >index? UIPageViewControllerNavigationDirectionReverse:UIPageViewControllerNavigationDirectionForward;
    
    

    
    //4.添加子控制器(一开始只需要添加一个 就可以实现无限)
    //最开始设置index默认为0
    //将频道的数组模型的第0个元素传过去
    WYNewListController *test =[[WYNewListController alloc]initWithIndex:index channelModel:channelView.channelModelArr[index]];
    
    NSArray *contronllers =@[test];
    
    ///setViewControllers 这个方法就是把屏幕显示的替换掉了
    
    //每次调用 setViewControllers 的方法都会将子控制器重新推一次
    [pageViewController setViewControllers:contronllers direction:direction animated:YES completion:nil];
}

//添加页
-(void)setPageViewController{
    //1.创建pageView控制器
     pageViewController =[[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:@{UIPageViewControllerOptionInterPageSpacingKey:@0}];
    
    //2.将pageView控制器添加到根控制器
    pageViewController.delegate =self;
    [self addChildViewController:pageViewController];
    //视图添加到根视图
    [self.view addSubview:pageViewController.view];
    [pageViewController didMoveToParentViewController:self];
    
    //调整约束
    [pageViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
       
        //上参照频道的下
        make.top.equalTo(channelView.mas_bottom);
        //左右参照当前View
        make.left.right.equalTo(self.view);
        //下参照当前的mas_bottomLayoutGuide
        make.bottom.equalTo(self.mas_bottomLayoutGuide);
        
    }];
    
    //3.添加子控制器(一开始只需要添加一个 就可以实现无限)
    //最开始设置index默认为0
    //将频道的数组模型的第0个元素传过去
    WYNewListController *test =[[WYNewListController alloc]initWithIndex:0 channelModel:channelView.channelModelArr[0]];
    
    NSArray *contronllers =@[test];
    [pageViewController setViewControllers:contronllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
    
    //4.实现数据源方法
    pageViewController.dataSource =self;
}
//生成前一个控制器
/*
 @param pageViewController 当前的pageViewController
 @param viewController 当前屏幕上展的子控制器
 @return 生成并返回前一个控制器
 */
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(WYNewListController *)viewController{
    //先判断 当前屏幕上展的子控制器 的index == 0
    //成立就是到头了 不再滚了
    if (viewController.index ==0) {
        return nil;
    }
    //拿到当前的index
    NSInteger  index =viewController.index-1;
    
    //设置为前一个控制器的index -1
    WYNewListController *tes =[[WYNewListController alloc]initWithIndex:index channelModel:channelView.channelModelArr[index]];
    return tes;
}
//生成后一个控制器
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(WYNewListController *)viewController{
    //判断当前屏幕上展的子控制器的index == 频道的模型数组个数-1
    //成立就到尾了
    if (viewController.index ==channelView.channelModelArr.count -1) {
        return nil;
    }
    //拿到当前的index
    NSInteger currentIndex =viewController.index+1;
    //设置为后一个控制器的index +1
    WYNewListController *tes =[[WYNewListController alloc]initWithIndex:currentIndex channelModel:channelView.channelModelArr[currentIndex]];
    
    return tes;
}

#pragma mark -UIPageViewController代理方法


/**
 pageViewController的子控制器将要 滑动  的时候调用

 @param pageViewController 当前的pageViewController
 @param pendingViewControllers  将要跳转到的子控制器的数组((里面装的将要跳转到的子控制器))
 */
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<WYNewListController *> *)pendingViewControllers NS_AVAILABLE_IOS(6_0){
    
    //获取将要跳转的子控制器
    WYNewListController *newList =pendingViewControllers[0];
    //2.然后将这个子控制器的index 赋值给频道的选中index 属性
    channelView.selectedIndex =newList.index;
}


/**
 
 pageViewController的子控制器滑动 结束 的时候调用
 
 @param _pageViewController 当前的pageViewController
 @param finished 动画是否结束
 @param previousViewControllers  从哪个子控制器滑动来的, 数组(里面装的是前一个子控制器)
 @param completed 是否结束
 */
- (void)pageViewController:(UIPageViewController *)_pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed{
    
    //1. 获取到当前的子控器的index
    //1.1先取到当前控制器 (第0个元素)
    WYNewListController *newList =(WYNewListController *)_pageViewController.viewControllers[0];
    //2.然后将这个子控制器的index 赋值给频道的选中index 属性
    channelView.selectedIndex =newList.index;
    
}


@end
